package com.autoglm.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.autoglm.data.model.Task
import com.autoglm.data.model.TaskStatus
import com.autoglm.ui.theme.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * 任务卡片组件
 * 显示任务信息和状态
 */
@Composable
fun TaskCard(
    task: Task,
    modifier: Modifier = Modifier,
    onClick: () -> Unit = {}
) {
    Card(
        modifier = modifier
            .fillMaxWidth()
            .clickable { onClick() },
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = Color.Transparent
        )
    ) {
        Box(
            modifier = Modifier
                .fillMaxWidth()
                .background(
                    brush = Brush.linearGradient(
                        colors = listOf(
                            GradientCardStart,
                            GradientCardEnd
                        )
                    ),
                    shape = RoundedCornerShape(16.dp)
                )
                .border(
                    width = 1.dp,
                    brush = Brush.linearGradient(
                        colors = listOf(
                            Accent500.copy(alpha = 0.3f),
                            Accent300.copy(alpha = 0.1f)
                        )
                    ),
                    shape = RoundedCornerShape(16.dp)
                )
                .padding(16.dp)
        ) {
            Column(
                verticalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                // 任务标题和状态
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    Text(
                        text = task.description,
                        style = MaterialTheme.typography.titleMedium,
                        color = Accent300,
                        fontWeight = FontWeight.SemiBold,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis,
                        modifier = Modifier.weight(1f)
                    )
                    
                    TaskStatusChip(status = task.status)
                }
                
                // 用户输入
                Text(
                    text = task.userInput,
                    style = MaterialTheme.typography.bodyMedium,
                    color = Neutral300,
                    maxLines = 2,
                    overflow = TextOverflow.Ellipsis
                )
                
                // 底部信息
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    // 时间信息
                    Text(
                        text = formatTaskTime(task.createdAt),
                        style = MaterialTheme.typography.bodySmall,
                        color = Neutral500
                    )
                    
                    // 步骤数量
                    Row(
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.spacedBy(4.dp)
                    ) {
                        Icon(
                            imageVector = Icons.Default.List,
                            contentDescription = "步骤数量",
                            tint = Neutral500,
                            modifier = Modifier.size(16.dp)
                        )
                        Text(
                            text = "${task.steps.size} 步骤",
                            style = MaterialTheme.typography.bodySmall,
                            color = Neutral500
                        )
                    }
                }
            }
        }
    }
}

/**
 * 任务状态芯片
 */
@Composable
private fun TaskStatusChip(status: TaskStatus) {
    val (text, color, icon) = when (status) {
        TaskStatus.PENDING -> Triple("待执行", Neutral500, Icons.Default.Schedule)
        TaskStatus.IN_PROGRESS -> Triple("执行中", Accent500, Icons.Default.PlayArrow)
        TaskStatus.COMPLETED -> Triple("已完成", Success500, Icons.Default.Check)
        TaskStatus.FAILED -> Triple("失败", Error500, Icons.Default.Error)
        TaskStatus.CANCELLED -> Triple("已取消", Warning500, Icons.Default.Cancel)
    }
    
    Surface(
        shape = RoundedCornerShape(12.dp),
        color = color.copy(alpha = 0.2f),
        modifier = Modifier.border(
            width = 1.dp,
            color = color.copy(alpha = 0.5f),
            shape = RoundedCornerShape(12.dp)
        )
    ) {
        Row(
            modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.spacedBy(4.dp)
        ) {
            Icon(
                imageVector = icon,
                contentDescription = null,
                tint = color,
                modifier = Modifier.size(12.dp)
            )
            Text(
                text = text,
                style = MaterialTheme.typography.labelSmall,
                color = color,
                fontWeight = FontWeight.Medium
            )
        }
    }
}

/**
 * 格式化任务时间
 */
private fun formatTaskTime(date: Date): String {
    val now = Date()
    val diff = now.time - date.time
    
    return when {
        diff < 60_000 -> "刚刚"
        diff < 3600_000 -> "${diff / 60_000}分钟前"
        diff < 86400_000 -> "${diff / 3600_000}小时前"
        else -> SimpleDateFormat("MM-dd HH:mm", Locale.getDefault()).format(date)
    }
}
